/*
 *
 *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
 *  * +----------------------------------------------------------------------
 *  * | Author: xw2sy@163.com
 *  * +----------------------------------------------------------------------
 *
 *  Copyright [2024] [OPENIITA]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * /
 */

package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.manager.IMapFenceData;
import cc.iotkit.manager.dto.bo.map.MapFenceBo;
import cc.iotkit.manager.dto.vo.map.MapFenceVo;
import cc.iotkit.manager.service.IMapFenceDevicesService;
import cc.iotkit.manager.service.IMapFenceService;
import cc.iotkit.manager.service.IMapService;
import cc.iotkit.model.map.MapFence;
import cc.iotkit.model.map.MapFenceDevices;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户Service业务层处理
 *
 * @author Michelle.Chung
 */
@RequiredArgsConstructor
@Service
public class MapFenceServiceImpl implements IMapFenceService {
    private final IMapService mapService;
    private final IMapFenceData sysMapFenceData;
    private final IMapFenceDevicesService mapFenceDevicesService;

    public MapFenceVo queryById(Long id) {
        return MapstructUtils.convert(sysMapFenceData.findById(id),MapFenceVo.class);
    }

    /**
     * 新增保存角色信息
     *
     * @param bo 角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(MapFenceBo bo) {
        MapFence entity=new MapFence();
        try {
            if (bo.getId() == null) {
                if (mapService.createFenceCircle(bo))
                    entity= sysMapFenceData.save(bo.to(MapFence.class));
            } else {
                if (mapService.updateFenceCircle(bo))
                    entity= sysMapFenceData.save(bo.to(MapFence.class));
            }

            //保存设备子表
            List<MapFenceDevices> devices=bo.getDevices();
            mapFenceDevicesService.saveAll(devices,entity.getId(),entity.getGfid());

            return true;
        }catch (Exception e){
            if(e instanceof BizException){
                if(((BizException) e).getCode().equals(10000002)){
                    throw new BizException(e.getMessage());
                }
            }
            mapService.deleteFence(bo.getGfid().toString());
            throw new BizException(e.getMessage());
        }
    }

    @Override
    public boolean deleteById(Long id) {
        sysMapFenceData.deleteById(id);
        return true;
    }

    @Transactional
    public boolean deleteByIds(Collection<Long> ids) {
        try {
            Collection<MapFence> mapFences = sysMapFenceData.findByIds(ids);
            String gfids = mapFences.stream().map(m -> m.getGfid().toString()).collect(Collectors.joining(", "));
            //删除子表明细和对应的高德监测对象
            mapFenceDevicesService.deleteByPids(ids);
            //删除高德电子围栏
            mapService.deleteFence(gfids);
            //删除电子围栏主表
            sysMapFenceData.deleteByIds(ids);
            return true;
        }catch (Exception e){
            throw new BizException(e.getMessage());
        }
    }

    @Override
    public Paging<MapFenceVo> queryPageList(PageRequest<MapFenceBo> request) {
        Paging<MapFence> pages=sysMapFenceData.findAllByConditions(request.to(MapFence.class));
        return pages.to(MapFenceVo.class);
    }

    @Override
    public List<MapFenceVo> queryList(MapFenceBo bo) {
        List<MapFence> list=sysMapFenceData.findAllByCondition(bo.to(MapFence.class));
        return MapstructUtils.copyListToList(list,MapFenceVo.class);
    }



}
